//
//  DDPositionCell.m
//  DingDongZP
//
//  Created by 杨政涛 on 16/7/8.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

#import "DDPositionCell.h"
//#import "DDUtils.h"

@implementation DDPositionCell


- (void) setDetail:(DDJobsDetail *)detail {
    
    _detail = detail;
    
    [self updateFrame];
    
}

- (void) updateFrame {
    
    self.position.text =  _detail.jobtitle;
    self.salary.text = _detail.salary;
    self.city.attributedText = [self string:_detail.city withIcon:[UIImage imageNamed:@"icon_address"]];
    self.year.attributedText = [self string:_detail.experience withIcon:[UIImage imageNamed:@"icon_experience"]];
    self.education.attributedText = [self string:_detail.edu withIcon:[UIImage imageNamed:@"icon_education"]];
    self.positionContentDescription.attributedText = [DDUtils attrWithString:8 font:[UIFont systemFontOfSize:12] text:_detail.jobdesc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1.000];
        self.bgView = [[UIView alloc]init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 5;
        [self.contentView addSubview:self.bgView];
        
        WEAKSELF
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf);
            make.left.equalTo(@(15));
            make.right.equalTo(weakSelf).offset(-15);
            make.bottom.equalTo(weakSelf).offset(-10);
        }];
        
        self.position = [[UILabel alloc]init];
        self.position.textColor = COLOR_FONT_MAIN_GREEN;
        self.position.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.position];
        [self.position mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(10);
            make.left.equalTo(@(15));
            make.right.equalTo(self.bgView).offset(-130);
            make.height.equalTo(@(30));
        }];
        
        self.salary = [[UILabel alloc]init];
        self.salary.textColor = COLOR_FONT_RED;
        self.salary.font = [UIFont systemFontOfSize:16];
        self.salary.textAlignment = 2;
        [self.bgView addSubview:self.salary];
        [self.salary mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(10);
            make.left.equalTo(self.position.mas_right).offset(10);
            make.right.equalTo(self.bgView).offset(-15);
            make.height.equalTo(@(30));
        }];
        
        self.city = [[UILabel alloc]init];
        self.city.textColor = COLOR_FONT_LIGHT_GRAY;
        self.city.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.city];
        [self.city mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(@(15));
            make.height.equalTo(@(30));
        }];
        
        self.year = [[UILabel alloc]init];
        self.year.textColor = COLOR_FONT_LIGHT_GRAY;
        self.year.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.year];
        [self.year mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(self.city.mas_right).offset(5);
            make.height.equalTo(@(30));
        }];
        
        
        self.education = [[UILabel alloc]init];
        self.education.textColor = COLOR_FONT_LIGHT_GRAY;
        self.education.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.education];
        [self.education mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(self.year.mas_right).offset(5);
            make.height.equalTo(@(30));
        }];
        
        UIView *seprateView1 = [[UIView alloc]init];
        seprateView1.backgroundColor = COLOR_FONT_LIGHT_GRAY;
        [self.bgView addSubview:seprateView1];
        [seprateView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.city.mas_bottom).offset(10);
            make.left.equalTo(@(0));
            make.right.equalTo(self.bgView);
            make.height.equalTo(@(0.5));
        }];
        
        self.positionDescription = [[UILabel alloc]init];
        self.positionDescription.textColor = COLOR_FONT_MAIN_GREEN;
        self.positionDescription.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.positionDescription];
        [self.positionDescription mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(seprateView1.mas_bottom).offset(10);
            make.left.equalTo(@(15));
            make.height.equalTo(@(30));
        }];
        self.positionDescription.text = @"职位描述";
        
        self.positionContentDescription = [[UILabel alloc]init];
        self.positionContentDescription.textColor = COLOR_FONT_BLACK;
        self.positionContentDescription.font = [UIFont systemFontOfSize:12];
        self.positionContentDescription.numberOfLines = 0;
        [self.bgView addSubview:self.positionContentDescription];
        [self.positionContentDescription mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.positionDescription.mas_bottom).offset(5);
            make.left.equalTo(@(15));
            make.right.equalTo(self.bgView).offset(-15);
            make.height.equalTo(@(70));
        }];
        
        self.seprateView2 = [[UIView alloc]init];
        self.seprateView2.backgroundColor = COLOR_FONT_LIGHT_GRAY;
        [self.bgView addSubview:self.seprateView2];
        [self.seprateView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.positionContentDescription.mas_bottom).offset(10);
            make.left.equalTo(@(0));
            make.right.equalTo(self.bgView);
            make.height.equalTo(@(0.5));
        }];
        
        self.openOrClose = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.openOrClose setTitle:@"展开" forState:UIControlStateNormal];
        [self.openOrClose setTitleColor:COLOR_FONT_MAIN_GREEN forState:UIControlStateNormal];
        [self.bgView addSubview:self.openOrClose];
        [self.openOrClose mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.seprateView2.mas_bottom);
            make.left.equalTo(@(0));
            make.right.equalTo(self.bgView);
            make.height.equalTo(@(45));
        }];
    }
    return self;
}

+(instancetype)loadTableView:(UITableView *)tableView
{
    NSString *simpleIdentify = @"DDPositionCell";
    DDPositionCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
    if(cell == nil)
    {
        cell = [[DDPositionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (NSAttributedString *)string:(NSString *)string withIcon:(UIImage *)icon
{
    if (string == nil || icon == nil) {
        return [NSAttributedString new];
    }
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:string];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:COLOR_FONT_DARK_GRAY
                            range:NSMakeRange(0,string.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.image = icon;
    
    textAttachment.bounds = CGRectMake(0, 0,12,12);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:0];
    [titleAttributed insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
    return titleAttributed;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
